﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace ColorGrabber
{
    static class Program
    {
        private static NotifyIcon icon = new NotifyIcon();
        private static ContextMenu iconMenu = new ContextMenu();
        private static ToolTip iconToolTip = new ToolTip();
        private static ColorGrabberFRM colorForm = null;

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            icon.Icon = Properties.Resources.ColorPickIcon;
            
            //Set up the context menu on the app tray icon
            iconMenu.MenuItems.Add(new MenuItem("Capture"));
            iconMenu.MenuItems[iconMenu.MenuItems.Count - 1].ShowShortcut = true;
            iconMenu.MenuItems[iconMenu.MenuItems.Count - 1].Click += OnCaptureClick;
            iconMenu.MenuItems.Add(new MenuItem("Exit"));
            iconMenu.MenuItems[iconMenu.MenuItems.Count - 1].ShowShortcut = true;
            iconMenu.MenuItems[iconMenu.MenuItems.Count - 1].Click += OnExitClick;

            icon.DoubleClick += OnCaptureClick;
            icon.ContextMenu = iconMenu;
            icon.Visible = true;

            LaunchCapture();
            Application.Run();
        }

        private static void LaunchCapture(bool toggleCaptureModeOn = false)
        {
            if (colorForm == null || colorForm.IsDisposed)
            {
                colorForm = new ColorGrabberFRM();
                //put the starting position in the bottom right corner of the main screen
                colorForm.StartPosition = FormStartPosition.Manual;
                colorForm.Left = Screen.PrimaryScreen.Bounds.X + Screen.PrimaryScreen.Bounds.Width - colorForm.Width - 20;
                colorForm.Top = Screen.PrimaryScreen.Bounds.Y + Screen.PrimaryScreen.Bounds.Height - colorForm.Height - 80;
            }
            if (toggleCaptureModeOn) colorForm.ToggleCapture(true);
            colorForm.Show();
        }

        private static void OnExitClick(object sender, EventArgs e)
        {
            icon.Dispose();
            Application.Exit();
        }

        private static void OnCaptureClick(object sender, EventArgs e)
        {
            LaunchCapture(true);
        }

    }
}
